package com.example.rjb_shoping.controller;

import com.example.rjb_shoping.config.RjbConstant;
import com.example.rjb_shoping.entity.RjbSpkie;
import com.example.rjb_shoping.query.SeckillRequest;
import com.example.rjb_shoping.query.SpkieQuery;
import com.example.rjb_shoping.service.RjbSpkieService;
import com.example.rjb_shoping.util.JsonUtil;
import com.example.rjb_shoping.util.RedisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.xq.softcup.mq.client.message.MqMessage;
import org.xq.softcup.mq.client.producer.MqProducer;

import javax.annotation.Resource;
import java.util.List;

/**
 * (RjbSpkie)秒杀控制层
 *
 * @author makejava
 * @since 2021-06-09 20:43:27
 */
@RestController
    @RequestMapping("rjbSpkie")
public class RjbSpkieController {
    /**
     * 服务对象
     */
    @Resource
    private RjbSpkieService rjbSpkieService;

    @Resource
    RedisUtils redisUtils;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public RjbSpkie selectOne(Integer id) {
        return this.rjbSpkieService.queryById(id);
    }

    /**
     * 秒杀列表分页查询
     * @param query
     * @return
     */
    @ApiOperation("秒杀列表")
    @GetMapping("selectList")
    public PageInfo selectList(SpkieQuery query) {
        PageHelper.startPage(query.getPageNum(), query.getPageSize());
        List<RjbSpkie> rjbSpkies = this.rjbSpkieService.queryAllByLimit(query);
        PageInfo pageInfo = new PageInfo(rjbSpkies);
        return pageInfo;
    }

    @ApiOperation("今日秒杀列表")
    @GetMapping("getTodaySeckills")
    public List<RjbSpkie> getTodaySeckills() {
        return rjbSpkieService.getTodaySeckills();
    }

    /**
     * 点击抢购按钮接口, 抢购商品只限购1个
     * @param  request 用户请求对象
     * @return  返回是否请求成功json
     */
    @ApiOperation("点击抢购接口")
    @PostMapping("doSeckill")
    public JsonUtil doSeckill(SeckillRequest request) {
        JsonUtil result = null;
        if (request.getCustomerId() == null) {
            result = new JsonUtil(100, "用户ID不能为空!");
        } else if (request.getMId() == null) {
            result = new JsonUtil(100, "请确认要购买的商品!");
        } else if (request.getSId() == null) {
            result = new JsonUtil(100, "秒杀ID不能为空!");
        } else {//当前用户提交次数

            //给当前用户在redis中添加排队次数,  判断是否已排队,防止脚本刷单、重复提交等
            long currentUserQueue = redisUtils.hincr(RjbConstant.SECKILL_USER_QUEUE_COUNT+request.getSId()+request.getMId(), request.getCustomerId().toString(), 1l);
//            Object currentUserQueue = redisUtils.hget(RjbConstant.SECKILL_USER_QUEUE_COUNT+sId+request.getMId(), request.getCustomerId().toString());
            if(currentUserQueue > 1){
                result = new JsonUtil(100, "您已提交了订单，请误重复提交!");
            }
//        从redis库存中预减库存
        Object secId = redisUtils.lrPop(RjbConstant.TODAY_SECKILL_STOCK + request.getSId() +":"+ request.getMId());
        //如果secId为空表示库存已售完
        if (secId == null) {
            //删除该用户的排队信息
            redisUtils.hdel(RjbConstant.SECKILL_USER_QUEUE_COUNT+request.getSId()+":"+request.getMId(), request.getCustomerId().toString());
            result = new JsonUtil(400, "该商品已售罄!");
        }

            //发送MQ消息,  数据格式:用户ID-秒杀ID-商品ID
//            StringBuffer data = new StringBuffer().append(request.getCustomerId()).append("-").append(request.getSId()).append("-").append(request.getMId());
//            MqProducer.produce(new MqMessage(RjbConstant.TOPIC_SECKILL, data.toString()), true); //异步发送消息
            String orderNum = redisUtils.generateOrderNo(RjbConstant.ORDER_PREFIX);
            result = new JsonUtil(200, "", orderNum);
        }
        return result;
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("queryByList")
    public List<RjbSpkie> queryByList() {
        return this.rjbSpkieService.queryByList();
    }

    /**
     * 新增秒杀规则
     * @param rjbSpkie
     * ?
     * @return
     */
    @GetMapping("release")
    public RjbSpkie release(RjbSpkie rjbSpkie){
        //System.out.println("rjbSpkie"+rjbSpkie);
        return rjbSpkieService.insert(rjbSpkie);
    }
}
